﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;



namespace School.Pages.Admission
{
    public partial class TestAppointment : System.Web.UI.Page
    {
        IEnumerable<TE_Student> Studentslist;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1017) == true)
                {
                    if (!(Page.IsPostBack))
                    {

                        btnSave.Attributes["OnClick"] = "javascript:return " + "confirm('سيتم حذف كل المواعيد السابقة و نتائج الاختبارات الخاصة بهذا الاختبار');";
                        Fillcrls();
                        InitializeControls();
                        GridDataBind();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();
            if (ddlSearchTest_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Test_Id", ddlSearchTest_Id.SelectedValue.ToString());
            }

            if (ddlSearchTeacher_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Teacher_Id", ddlSearchTeacher_Id.SelectedValue.ToString());
            }
            SearchTable.Add("Passed", "1");
            IEnumerable<ADM_Test_Appointment> results = Test_Appointment.All(SearchTable);

            griddata.DataSource = results;

            griddata.DataBind();

            griddata.Visible = true;
            mycount = results.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {
            ddlSearchTeacher_Id.DataSource = Test_Teacher.GetTeachers();
            ddlSearchTeacher_Id.DataTextField = "Name";
            ddlSearchTeacher_Id.DataValueField = "Id";
            ddlSearchTeacher_Id.DataBind();
            ddlSearchTeacher_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlcampus_Id.DataSource = Structure.GetCampuses();
            ddlcampus_Id.DataTextField = "Name";
            ddlcampus_Id.DataValueField = "Id";
            ddlcampus_Id.DataBind();
            ddlcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlCertificate_Id.DataSource = Structure.GetCertificates();
            ddlCertificate_Id.DataTextField = "Name";
            ddlCertificate_Id.DataValueField = "Id";
            ddlCertificate_Id.DataBind();
            ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchCertificate_Id.DataSource = Structure.GetCertificates();
            ddlSearchCertificate_Id.DataTextField = "Name";
            ddlSearchCertificate_Id.DataValueField = "Id";
            ddlSearchCertificate_Id.DataBind();
            ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchGrade_ID.DataSource = Structure.GetGrades();
            ddlSearchGrade_ID.DataTextField = "Name";
            ddlSearchGrade_ID.DataValueField = "id";
            ddlSearchGrade_ID.DataBind();
            ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlGrade_Id.DataSource = Structure.GetGrades();
            ddlGrade_Id.DataTextField = "Name";
            ddlGrade_Id.DataValueField = "id";
            ddlGrade_Id.DataBind();
            ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchStage_Id.DataSource = Structure.GetStages();
            ddlSearchStage_Id.DataTextField = "Name";
            ddlSearchStage_Id.DataValueField = "id";
            ddlSearchStage_Id.DataBind();
            ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlStage_Id.DataSource = Structure.GetStages();
            ddlStage_Id.DataTextField = "Name";
            ddlStage_Id.DataValueField = "id";
            ddlStage_Id.DataBind();
            ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlEductionalYear_Id.DataSource = Structure.GetCurrentEductionalYear();
            ddlEductionalYear_Id.DataTextField = "Name";
            ddlEductionalYear_Id.DataValueField = "id";
            ddlEductionalYear_Id.DataBind();

            ddlSearchEductionalYear_Id.DataSource = Structure.GetCurrentEductionalYear();
            ddlSearchEductionalYear_Id.DataTextField = "Name";
            ddlSearchEductionalYear_Id.DataValueField = "id";
            ddlSearchEductionalYear_Id.DataBind();
            ddlSearchEductionalYear_Id.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlSearchTest_Id.DataSource = ADMTest.All().Select(x => new { Date = x.FromDate.ToShortDateString() + " " + x.ORG_Campus.Name.ToString() + "-" + x.ORG_Certificate.Name.ToString() + "-" + x.ORG_Stage.Name.ToString() + "-" + x.ORG_Grade.Name.ToString(), id = x.Id });
            ddlSearchTest_Id.DataTextField = "Date";
            ddlSearchTest_Id.DataValueField = "id";
            ddlSearchTest_Id.DataBind();
            ddlSearchTest_Id.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlTest_Id.DataSource = ADMTest.All().Select(x => new { Date = x.FromDate.ToShortDateString() + " " + x.ORG_Campus.Name.ToString() + "-" + x.ORG_Certificate.Name.ToString() + "-" + x.ORG_Stage.Name.ToString() + "-" + x.ORG_Grade.Name.ToString(), id = x.Id });
            ddlTest_Id.DataTextField = "Date";
            ddlTest_Id.DataValueField = "id";
            ddlTest_Id.DataBind();
            ddlTest_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            ddlTest_Id.SelectedIndex = 0;
            ddlTeacher_Id.Items.Clear();
            ddlSubject_Id.Items.Clear();
            ddlcampus_Id.SelectedIndex = 0;
            ddlCertificate_Id.SelectedIndex = 0;
            ddlStage_Id.SelectedIndex = 0;
            ddlGrade_Id.SelectedIndex = 0;
            lblTeacherCount.Text = "";
            lblSubjectCount.Text = "";
            lblTeacherss.Visible = false;
            lblMins.Visible = false;
            lblMessageAdd.Text = "";
            lblMessage.Text = "";
            lblCountOfTestStudents.Text = "";
            Session["Students"] = "";
        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {

        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = Test_Appointment.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "تم حذف البيانات بنجاح";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "  لم يتم حذف البيانات بنجاح رجاء مراجعة مدير النظام";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل ترغب في حذف هذا الطالب من اختبار القبول (" +
                DataBinder.Eval(e.Row.DataItem, "TE_Student.Ar_FirstName") + "  " + DataBinder.Eval(e.Row.DataItem, "TE_Student.Ar_SecondName") + "  " + DataBinder.Eval(e.Row.DataItem, "TE_Student.Ar_ThirdNmae") + ") ?')");
                
            }
            //int i;
            //for (i = 0; i < griddata.Rows.Count; i++)
            //{
            //    Label lblStudentId;
            //    lblStudentId = ((Label)(griddata.Rows[i].FindControl("lblStudentId")));
            //    TE_Student student = Student.Single(long.Parse(lblStudentId.Text));
            //    if(student.Status_Id != 1 && student.Status_Id != 3)
            //    {
            //        griddata.Rows[i].Visible= false;
            //    }

            //}
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;
            PnlResults.Visible = true;
            
            DetailsView1bind();
           
        }

        protected void DetailsView1bind()
        {
            lblMessageDetails.Text = "";
            Hashtable SearchTable = new Hashtable();

            
            ADM_Test_Appointment app=  Test_Appointment.Single(long.Parse(griddata.SelectedDataKey.Value.ToString())); 
            SearchTable.Add("Student_Id",app.Student_Id );
            SearchTable.Add("Test_Id", app.Test_Id);
            IEnumerable<ADM_Student_Subject> studentsubjects = Student_Subject.All(SearchTable);
            ResultsGridView.DataSource = studentsubjects ;

            ResultsGridView.DataBind();
           
            lblStudentName.Text = studentsubjects.ElementAt(0).TE_Student.Ar_FirstName + " " + studentsubjects.ElementAt(0).TE_Student.Ar_SecondName + " " + studentsubjects.ElementAt(0).TE_Student.Ar_ThirdNmae + " " + studentsubjects.ElementAt(0).TE_Student.Ar_FourthName;
            Session["StudentId"] = studentsubjects.ElementAt(0).Student_Id.ToString();
            Session["EductionalYear"] = studentsubjects.ElementAt(0).ADM_Test.EductionalYear_Id.ToString();  
        }



        # endregion



        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            InitializeControls();
        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;
            pnldetails.Visible = false;
            InitializeControls();
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
            PnlResults.Visible = false;
            GridDataBind();
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Studentslist = (IEnumerable<TE_Student>)Session["Students"];
            if (Studentslist.Count() == 0)
            {
                lblMessageAdd.Text = "لا يوجد طلا ب متاحين لهذا الاختبار";
                return;
            }
            
            if (ddlTest_Id.SelectedIndex == 0)
            {
                lblMessageAdd.Text = "رجاء اختيار اختبار القبول";
                return;
            }
            if (ddlTeacher_Id.Items.Count == 0)
            {
                lblMessageAdd.Text = "لم يتم ادخال مدرسين لعمل الاختبار";
                return;
            }
            if (ddlSubject_Id.Items.Count == 0)
            {
                lblMessageAdd.Text = "لم يتم ادخال مواد لعمل الاختبار";
                return;
            }
            Int64 Test_Id = Convert.ToInt64(ddlTest_Id.SelectedValue);
            Int32 Teacher;

            long xx = 0 ;
            Int32 Tcounter,DCounter,Studentexamtime,StudentCounter,SubjectCounter;
            Int32 ExamTime = 0;
            
            DateTime ExamDate= Convert.ToDateTime(lblFromDate.Text)  ;
            StudentCounter = 0;
            DCounter = 0;
                Tcounter = 0;
                if (Test_Appointment.checkOldApp(long.Parse(ddlTest_Id.SelectedValue)) == false)
                {
                    lblMessageAdd.Text = "خطأ ..لم يتمكن النظام من  حذف المواعيد السابقة";
                    return;
                }
            while (Tcounter < Convert.ToInt32(lblTeacherCount.Text))
            {
                DCounter = 0;
                Teacher = Convert.ToInt32(ddlTeacher_Id.Items[Tcounter].Value);
                while ( DCounter < Convert.ToInt32(lblDaysCount.Text))
                {
                    ExamDate = ExamDate.AddDays(DCounter);

                        ExamTime = 0;
                        
                            while (StudentCounter < Studentslist.Count())
                            {
                                SubjectCounter = 0;

                                xx =Test_Appointment.Add(new ADM_Test_Appointment()
                                {
                                    Test_Id = Test_Id,
                                    Teacher_Id = Teacher,
                                    Student_Id = Studentslist.ElementAt(StudentCounter).Id,
                                    Date = ExamDate,
                                    EductionalYear_Id = Convert.ToInt32(ddlEductionalYear_Id.SelectedValue),
                                    Time = Convert.ToDateTime("8:00").AddMinutes(ExamTime ) ,
                                    Degree = 0,
                                    Comment = "ادخال جديد"

                                });

                                while (SubjectCounter < ddlSubject_Id.Items.Count)
                                {
                                    Student_Subject.Add(new ADM_Student_Subject()
                                    {
                                        Student_Id = Studentslist.ElementAt(StudentCounter).Id,
                                        Test_Id = Test_Id,
                                        Subject_Id = long.Parse(ddlSubject_Id.Items[SubjectCounter].Value),
                                        Degree= 0
                                    });
                                    SubjectCounter += 1;
                                }

                            ExamTime += Convert.ToInt32(lblSubjectCount.Text);

                            StudentCounter += 1;   
                            if (StudentCounter == Studentslist.Count())
                            {
                                break; 
                            }
                            if (ExamTime >= 480)
                            {
                                break;
                            }
                           
                    }
                            DCounter += 1;
                }
                Tcounter += 1;
            }

            if (xx != -1)
            {
                
                InitializeControls();
                lblMessageAdd.Text = "تم حفظ البيانات بنجاح";
            }
            else
            {
                lblMessageAdd.Text = "لم يتم حفظ البيانات";
            }

        }


        # endregion



        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {


            GridDataBind();
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlGrade_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtShowCampus_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {

        }

        protected void ddlTest_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ADM_Test test = ADMTest.Single(Convert.ToInt64(ddlTest_Id.SelectedValue));
            if (test != null)
            {
                ddlcampus_Id.SelectedIndex = ddlcampus_Id.Items.IndexOf(ddlcampus_Id.Items.FindByValue(test.Campus_Id.ToString()));
                ddlCertificate_Id.SelectedIndex = ddlCertificate_Id.Items.IndexOf(ddlCertificate_Id.Items.FindByValue(test.Certificate_Id.ToString()));
                ddlStage_Id.SelectedIndex = ddlStage_Id.Items.IndexOf(ddlStage_Id.Items.FindByValue(test.Stage_Id.ToString()));
                ddlGrade_Id.SelectedIndex = ddlGrade_Id.Items.IndexOf(ddlGrade_Id.Items.FindByValue(test.Grade_Id.ToString()));

            }

            ddlTeacher_Id.DataTextField = "Name";
            ddlTeacher_Id.DataValueField = "Id";
            ddlTeacher_Id.DataSource = Test_Teacher.GetTestTeachers(Convert.ToInt32(ddlTest_Id.SelectedValue));
            ddlTeacher_Id.DataBind();
            lblTeacherCount.Visible = true;
            lblTeacherCount.Text = ddlTeacher_Id.Items.Count.ToString();

            IEnumerable<ADM_Test_Subject> subjects = Test_Subject.GetTestSubject(Convert.ToInt32(ddlTest_Id.SelectedValue));

            ddlSubject_Id.DataTextField = "Name";
            ddlSubject_Id.DataValueField = "Id";
            ddlSubject_Id.DataSource = subjects;
            ddlSubject_Id.DataBind();
            Int16 i,SubjectCount;
            DateTime dt1 = test.FromDate;
            DateTime dt2 = test.ToDate ;
            TimeSpan ts = dt2-dt1;
            lblDaysCount.Text  = Convert.ToString(ts.Days + 1);
            lblFromDate.Text = dt1.ToString();
            SubjectCount = 0;
            for (i = 0; i < subjects.Count() ; i++)
            {
                SubjectCount += Convert.ToInt16(subjects.ElementAt(i).Duration);
            }
            lblSubjectCount.Text = SubjectCount.ToString();
            lblSubjectCount.Visible = true;

            lblTeacherss.Visible = true;
            lblMins.Visible =true;
            Studentslist = Student.GetStudentsForExam(1, 3, long.Parse(ddlcampus_Id.SelectedValue), long.Parse(ddlCertificate_Id.SelectedValue), long.Parse(ddlStage_Id.SelectedValue), long.Parse(ddlGrade_Id.SelectedValue));
            Session["Students"] = Studentslist;
            lblCountOfTestStudents.Text = "عدد الطلاب لهذا الاختبار " + Studentslist.Count().ToString() + " طالب ";
        }

        protected void ddlSearchTest_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ADM_Test test = ADMTest.Single(Convert.ToInt64(ddlSearchTest_Id.SelectedValue));
            if (test != null)
            {
                ddlSearchcampus_Id.SelectedIndex = ddlSearchcampus_Id.Items.IndexOf(ddlSearchcampus_Id.Items.FindByValue(test.Campus_Id.ToString()));
                ddlSearchCertificate_Id.SelectedIndex = ddlSearchCertificate_Id.Items.IndexOf(ddlSearchCertificate_Id.Items.FindByValue(test.Certificate_Id.ToString()));
                ddlSearchStage_Id.SelectedIndex = ddlSearchStage_Id.Items.IndexOf(ddlSearchStage_Id.Items.FindByValue(test.Stage_Id.ToString()));
                ddlSearchGrade_ID.SelectedIndex = ddlSearchGrade_ID.Items.IndexOf(ddlSearchGrade_ID.Items.FindByValue(test.Grade_Id.ToString()));

            }
            else
            {
                ddlSearchcampus_Id.SelectedIndex = 0;
                ddlSearchCertificate_Id.SelectedIndex = 0;
                ddlSearchStage_Id.SelectedIndex = 0;
                ddlSearchGrade_ID.SelectedIndex = 0;
            }

        }

        protected void ddlTeacher_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ResultsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void ResultsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void ResultsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void ResultsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ResultsGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //ResultsGridView.EditIndex = e.NewEditIndex;
            //DetailsView1bind ();
        }

        protected void ResultsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //ADM_Student_Subject s = Student_Subject.Single(long.Parse(e.Keys[0].ToString()));
            //Student_Subject.Update(new ADM_Student_Subject
            //{
            //    Id = s.Id,
            //    Test_Id = s.Test_Id ,
            //    Student_Id=s.Student_Id ,
            //    Subject_Id= s.Subject_Id,
            //    Degree = Double.Parse(e.NewValues[0].ToString())
            //});
            //ResultsGridView.EditIndex = -1;
            //DetailsView1bind();
        }

        protected void ResultsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //ResultsGridView.EditIndex = -1;
            //DetailsView1bind();
        }

        protected void btnChangeStatus_Click(object sender, EventArgs e)
        {
            Student.UpdateStatusId(new TE_Student
            {
                Id = long.Parse(Session["StudentId"].ToString()),
                Status_Id = int.Parse(rblStatusType.SelectedValue )
            }
            );
            TE_Student mystudent = Student.Single(long.Parse(Session["StudentId"].ToString()));

            if (StudentTransaction.Add(new ADM_Student_Transaction
            {
                Student_Id = mystudent.Id,
                Status_Id = int.Parse(rblStatusType.SelectedValue),
                EductionalYear_Id = long.Parse(Session["EductionalYear"].ToString()),
                Campus_Id = mystudent.Campus_Id,
                Certificate_Id = mystudent.Certificate_Id,
                Stage_Id = mystudent.Stage_Id,
                Grad_Id = mystudent.Grade_ID
            }) != 0)
            {
                lblMessageDetails.Text = "تم تغيير حالة الطالب بنجاح";
            }
            else
            {
                lblMessageDetails.Text = "لم يتم تغيير حالة الطالب";
            }
            ;
        }

        protected void btnUpdateDegree_Click(object sender, EventArgs e)
        {

            int i ;
            try
            {

                for (i = 0; i < ResultsGridView.Rows.Count; i++)
                {
                    ADM_Student_Subject s = Student_Subject.Single(long.Parse(ResultsGridView.Rows[i].Cells[0].Text.ToString()));
                    TextBox txtDegree;


                    txtDegree = ((TextBox)(ResultsGridView.Rows[i].FindControl("Textbox1")));
                    Student_Subject.Update(new ADM_Student_Subject
                    {
                        Id = s.Id,
                        Test_Id = s.Test_Id,
                        Student_Id = s.Student_Id,
                        Subject_Id = s.Subject_Id,
                        Degree = Double.Parse(txtDegree.Text)
                    });

                }
                DetailsView1bind();
                lblMessageDetails.Text = "تم حفظ نتائج الطالب بنجاح";
            }
            catch
            {
                lblMessageDetails.Text = "حدث خطأ عند الحفظ";
            }

            
        }

        protected void btnbtnPrintReport_Click(object sender, EventArgs e)
        {
            if (ddlSearchTest_Id.SelectedIndex > 0)
            {
                Session["Test_Id"] = ddlSearchTest_Id.SelectedValue;
                string strbScript = "";
                strbScript = "<script language='javascript'>";
                strbScript = strbScript + "window.open('Reports/ShowAppointmentsReport.aspx', true, 'height=600,width=800,status=no,toolbar=no,menubar=no,location=no')";
                strbScript = strbScript + "</script>";

                Response.Write(strbScript);
            }
            else
            {
                lblMessage.Text = "برجاء اختيار احد الفصول من القائمة";
            }

        }

    }
}